# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.jsonUtils_v2.table2Json_Chen import Table2Json
from com.zzh.utils.pagingUtils import MyPaging
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
from com.zzh.utils.requestFieldMapDBField import fieldMap_Chen
import random
import time
localTime = time.strftime('%Y-%m-%d', time.localtime(time.time()))

class ViewItemDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def findInfo(self):
        """
        查询 view_city
        :return:
        """
        sql = "select * from view_item"
        cur = self.conn.cursor()
        rs = cur.execute(sql)
        info = None
        if rs:
            list0 = cur.fetchall()
            info = Table2Json.view_item_list_to_json(list0)
        cur.close()
        self.conn.close()
        return info

    def find_by_condition(self, **kwargs):
        """
        view_item 条件查询
        :param kwargs:
        :return:
        """
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']

        # 获取对应的映射字典
        fieldMap = fieldMap_Chen.viewItemFieldMap
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM view_item WHERE " + whereSentence + " and check_status_one=2 AND check_status_two=2 AND item_status=2 ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM view_item WHERE " + whereSentence + " and check_status_one=2 AND check_status_two=2 AND item_status=2"
        try:
            rs = cur.execute(sql)
        except:
            return -200, 0  # sql 语句执行错误

        if rs:
            adinfo = cur.fetchall()
            lists = MyPaging().getPage(rs, pageIndex, pageSize, adinfo)
            adJson = Table2Json.view_item_list_to_json(lists)
            return adJson, rs
        else:
            return None, 0



    def find_by_conditionOverDue0(self, **kwargs):
        """
        view_item 条件查询
        :param kwargs:
        :return:
        """
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']

        # 获取对应的映射字典
        fieldMap = fieldMap_Chen.viewItemFieldMap
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM view_item WHERE " + whereSentence + " and check_status_one=2 AND check_status_two=2 AND item_status=2 AND item_deadline >= '"+localTime+ "' ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM view_item WHERE " + whereSentence + " and check_status_one=2 AND check_status_two=2 AND item_status=2 AND item_deadline >= '"+localTime+"'"

        try:
            rs = cur.execute(sql)
        except:
            return -200, 0  # sql 语句执行错误

        if rs:
            adinfo = cur.fetchall()
            lists = MyPaging().getPage(rs, pageIndex, pageSize, adinfo)
            adJson = Table2Json.view_item_list_to_json(lists)
            return adJson, rs
        else:
            return None, 0

    def find_by_conditionOverDue1(self, **kwargs):
        """
        view_item 条件查询
        :param kwargs:
        :return:
        """
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']

        # 获取对应的映射字典
        fieldMap = fieldMap_Chen.viewItemFieldMap
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM view_item WHERE " + whereSentence + " and check_status_one=2 AND check_status_two=2 AND item_status=2 AND item_deadline < '" + localTime + "' ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM view_item WHERE " + whereSentence + " and check_status_one=2 AND check_status_two=2 AND item_status=2 AND item_deadline < '" + localTime+"'"
        try:
            rs = cur.execute(sql)
        except:
            return -200, 0  # sql 语句执行错误

        if rs:
            adinfo = cur.fetchall()
            lists = MyPaging().getPage(rs, pageIndex, pageSize, adinfo)
            adJson = Table2Json.view_item_list_to_json(lists)
            return adJson, rs
        else:
            return None, 0

    def find_by_condition2(self, **kwargs):
        """
        view_item 条件查询
        :param kwargs:
        :return:
        """
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']

        # 获取对应的映射字典
        fieldMap = fieldMap_Chen.viewItemFieldMap
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM view_item WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM view_item WHERE " + whereSentence
        try:
            rs = cur.execute(sql)
        except:
            return -200, 0    # sql 语句执行错误

        if rs:
            adinfo = cur.fetchall()
            lists = MyPaging().getPage(rs, pageIndex, pageSize, adinfo)
            adJson = Table2Json.view_item_list_to_json(lists)
            return adJson, rs
        else:
            return None, 0

    def getRandomItem(self, top):
        cur = self.conn.cursor()

        sql = "select * from view_item WHERE check_status_one=2 AND check_status_two=2 AND item_status=2 ORDER by rand() limit %s " % top
        rs = cur.execute(sql)
        if rs:
            rsSet = cur.fetchall()
            json2 = Table2Json.view_item_list_to_json(rsSet)
            return json2
        else:
            return None


